home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / Gnuplot / Source / term / post.trm < prev    next >
Encoding:
Text File  |  1993-03-02  |  10.8 KB  |  407 lines

  1. /*
  2.  * $Id: post.trm%v 3.38.2.74 1993/02/19 01:14:04 woo Exp woo $
  3.  */
  4.  
  5. /* GNUPLOT - post.trm */
  6. /*
  7.  * Copyright (C) 1990 - 1993   
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted, 
  11.  * provided that the above copyright notice appear in all copies and 
  12.  * that both that copyright notice and this permission notice appear 
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the modified code.  Modifications are to be distributed 
  17.  * as patches to released version.
  18.  *  
  19.  * This software  is provided "as is" without express or implied warranty.
  20.  * 
  21.  * This file is included by ../term.c.
  22.  *
  23.  * This terminal driver supports:
  24.  *     postscript
  25.  *
  26.  * AUTHORS
  27.  *  Russell Lang
  28.  * 
  29.  * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
  30.  *
  31.  * The 'postscript' driver produces landscape output 10" wide and 7" high.  
  32.  * To change font to Times-Roman and font size to 20pts use 
  33.  * 'set term postscript "Times-Roman" 20'.
  34.  * To get a smaller (5" x 3.5") eps output use 'set term post eps'
  35.  * and make only one plot per file.  Font size for eps will be half
  36.  * the specified size.
  37.  */
  38.  
  39.  
  40. /* PostScript driver by Russell Lang, rjl@monu1.cc.monash.edu.au */
  41.  
  42. char ps_font[MAX_ID_LEN+1] = "Helvetica" ; /* name of font */
  43. int ps_fontsize = 14;                     /* size of font in pts */
  44. TBOOLEAN ps_portrait = FALSE;                 /* vertical page */
  45. TBOOLEAN ps_color = FALSE;
  46. TBOOLEAN ps_eps = FALSE;    /* Is this for an eps file? */
  47.             /* Added by Robert Davis <davis@ecn.purdue.edu> */
  48. int ps_page=0;            /* page count */
  49. int ps_path_count=0;     /* count of lines in path */
  50. int ps_ang=0;            /* text angle */
  51. enum JUSTIFY ps_justify=LEFT;    /* text is flush left */
  52.  
  53. char GPFAR * GPFAR PS_header[] = {
  54. "/M {moveto} bind def\n",
  55. "/L {lineto} bind def\n",
  56. "/R {rmoveto} bind def\n",
  57. "/V {rlineto} bind def\n",
  58. "/vpt2 vpt 2 mul def\n",
  59. "/hpt2 hpt 2 mul def\n",
  60. /* flush left show */
  61. "/Lshow { currentpoint stroke M\n",
  62. "  0 vshift R show } def\n", 
  63. /* flush right show */
  64. "/Rshow { currentpoint stroke M\n",
  65. "  dup stringwidth pop neg vshift R show } def\n", 
  66. /* centred show */
  67. "/Cshow { currentpoint stroke M\n",
  68. "  dup stringwidth pop -2 div vshift R show } def\n", 
  69. /* Dash or Color Line */
  70. "/DL { Color {setrgbcolor 0 setdash }\n",
  71. " {pop pop pop 0 setdash} ifelse } def\n",
  72. /* Border Lines */
  73. "/BL { stroke gnulinewidth 2 mul setlinewidth } def\n",
  74. /* Axes Lines */
  75. "/AL { stroke gnulinewidth 2 div setlinewidth } def\n",
  76. /* Plot Lines */
  77. "/PL { stroke gnulinewidth setlinewidth } def\n",
  78. /* Line Types */
  79. "/LTb { BL [] 0 0 0 DL } def\n", /* border */
  80. "/LTa { AL [1 dl 2 dl] 0 setdash 0 0 0 setrgbcolor } def\n", /* axes */
  81. "/LT0 { PL [] 0 1 0 DL } def\n",
  82. "/LT1 { PL [4 dl 2 dl] 0 0 1 DL } def\n",
  83. "/LT2 { PL [2 dl 3 dl] 1 0 0 DL } def\n",
  84. "/LT3 { PL [1 dl 1.5 dl] 1 0 1 DL } def\n",
  85. "/LT4 { PL [5 dl 2 dl 1 dl 2 dl] 0 1 1 DL } def\n",
  86. "/LT5 { PL [4 dl 3 dl 1 dl 3 dl] 1 1 0 DL } def\n",
  87. "/LT6 { PL [2 dl 2 dl 2 dl 4 dl] 0 0 0 DL } def\n",
  88. "/LT7 { PL [2 dl 2 dl 2 dl 2 dl 2 dl 4 dl] 1 0.3 0 DL } def\n",
  89. "/LT8 { PL [2 dl 2 dl 2 dl 2 dl 2 dl 2 dl 2 dl 4 dl] 0.5 0.5 0.5 DL } def\n",
  90. "/P { stroke [] 0 setdash\n", /* Point */
  91. "  currentlinewidth 2 div sub M\n",
  92. "  0 currentlinewidth V stroke } def\n",
  93. "/D { stroke [] 0 setdash 2 copy vpt add M\n", /* Diamond */
  94. "  hpt neg vpt neg V hpt vpt neg V\n",
  95. "  hpt vpt V hpt neg vpt V closepath stroke\n",
  96. "  P } def\n",
  97. "/A { stroke [] 0 setdash vpt sub M 0 vpt2 V\n", /* Plus (Add) */
  98. "  currentpoint stroke M\n",
  99. "  hpt neg vpt neg R hpt2 0 V stroke\n",
  100. "  } def\n",
  101. "/B { stroke [] 0 setdash 2 copy exch hpt sub exch vpt add M\n", /* Box */
  102. "  0 vpt2 neg V hpt2 0 V 0 vpt2 V\n",
  103. "  hpt2 neg 0 V closepath stroke\n",
  104. "  P } def\n",
  105. "/C { stroke [] 0 setdash exch hpt sub exch vpt add M\n", /* Cross */
  106. "  hpt2 vpt2 neg V currentpoint stroke M\n",
  107. "  hpt2 neg 0 R hpt2 vpt2 V stroke } def\n",
  108. "/T { stroke [] 0 setdash 2 copy vpt 1.12 mul add M\n", /* Triangle */
  109. "  hpt neg vpt -1.62 mul V\n",
  110. "  hpt 2 mul 0 V\n",
  111. "  hpt neg vpt 1.62 mul V closepath stroke\n",
  112. "  P  } def\n",
  113. "/S { 2 copy A C} def\n", /* Star */
  114. NULL
  115. };
  116.  
  117. #define PS_XOFF    50    /* page offset in pts */
  118. #define PS_YOFF    50
  119.  
  120. #define PS_XMAX 7200
  121. #define PS_YMAX 5040
  122.  
  123. #define PS_XLAST (PS_XMAX - 1)
  124. #define PS_YLAST (PS_YMAX - 1)
  125.  
  126. #define PS_VTIC (PS_YMAX/80)
  127. #define PS_HTIC (PS_YMAX/80)
  128.  
  129. #define PS_SC (10)                /* scale is 1pt = 10 units */
  130. #define    PS_LW (0.5*PS_SC)        /* linewidth = 0.5 pts */
  131.  
  132. #define PS_VCHAR (14*PS_SC)        /* default is 14 point characters */
  133. #define PS_HCHAR (14*PS_SC*6/10)
  134.  
  135. int PS_pen_x, PS_pen_y;
  136. int PS_taken;
  137. int PS_linetype_last;
  138. TBOOLEAN PS_relative_ok;
  139.  
  140. PS_options()
  141. {
  142.     extern struct value *const_express();
  143.     extern double real();
  144.  
  145.     if (!END_OF_COMMAND) {
  146.         if (almost_equals(c_token,"p$ortrait")) {
  147.             ps_portrait=TRUE;
  148.              ps_eps=FALSE;
  149.             c_token++;
  150.         }
  151.         else if (almost_equals(c_token,"l$andscape")) {
  152.             ps_portrait=FALSE;
  153.              ps_eps=FALSE;
  154.             c_token++;
  155.         }
  156.          else if (almost_equals(c_token,"e$psf")) {
  157.              ps_portrait=TRUE;
  158.              ps_eps = TRUE;
  159.              c_token++;
  160.          }
  161.         else if (almost_equals(c_token,"d$efault")) {
  162.             ps_portrait=FALSE;
  163.              ps_eps=FALSE;
  164.             ps_color=FALSE;
  165.             strcpy(ps_font,"Helvetica");
  166.             ps_fontsize = 14;
  167.             term_tbl[term].v_char = (unsigned int)(ps_fontsize*PS_SC);
  168.             term_tbl[term].h_char = (unsigned int)(ps_fontsize*PS_SC*6/10);
  169.             c_token++;
  170.         }
  171.     }
  172.  
  173.     if (!END_OF_COMMAND) {
  174.         if (almost_equals(c_token,"m$onochrome")) {
  175.             ps_color=FALSE;
  176.             c_token++;
  177.         }
  178.         else if (almost_equals(c_token,"c$olor")) {
  179.             ps_color=TRUE;
  180.             c_token++;
  181.         }
  182.     }
  183.  
  184.     if (!END_OF_COMMAND && isstring(c_token)) {
  185.         quote_str(ps_font,c_token);
  186.         c_token++;
  187.     }
  188.  
  189.     if (!END_OF_COMMAND) {
  190.         /* We have font size specified */
  191.         struct value a;
  192.         ps_fontsize = (int)real(const_express(&a));
  193.         term_tbl[term].v_char = (unsigned int)(ps_fontsize*PS_SC);
  194.         term_tbl[term].h_char = (unsigned int)(ps_fontsize*PS_SC*6/10);
  195.     }
  196.  
  197.     sprintf(term_options,"%s %s \"%s\" %d",
  198.         ps_eps ? "eps" : (ps_portrait ? "portrait" : "landscape"),
  199.         ps_color ? "color" : "monochrome",ps_font,ps_fontsize);
  200. }
  201.  
  202.  
  203. PS_init()
  204. {
  205. struct termentry *t = &term_tbl[term];
  206. int i;
  207.     ps_page = 0;
  208.     if (!ps_eps)
  209.         fprintf(outfile,"%%!PS-Adobe-2.0\n");
  210.     else
  211.         fprintf(outfile,"%%!PS-Adobe-2.0 EPSF-2.0\n");
  212.     fprintf(outfile,"%%%%Creator: gnuplot\n");
  213.     fprintf(outfile,"%%%%DocumentFonts: %s\n", ps_font);
  214.     fprintf(outfile,"%%%%BoundingBox: %d %d ", PS_XOFF,PS_YOFF);
  215.     if (ps_portrait)
  216.         fprintf(outfile,"%d %d\n", 
  217.             (int)(xsize*(ps_eps ? 0.5 : 1.0)*(PS_XMAX)/PS_SC+0.5+PS_XOFF), 
  218.             (int)(ysize*(ps_eps ? 0.5 : 1.0)*(PS_YMAX)/PS_SC+0.5+PS_YOFF) );
  219.     else 
  220.         fprintf(outfile,"%d %d\n", 
  221.             (int)(ysize*(ps_eps ? 0.5 : 1.0)*(PS_YMAX)/PS_SC+0.5+PS_XOFF), 
  222.             (int)(xsize*(ps_eps ? 0.5 : 1.0)*(PS_XMAX)/PS_SC+0.5+PS_YOFF) );
  223.     if (!ps_eps)
  224.         fprintf(outfile,"%%%%Pages: (atend)\n");
  225.     fprintf(outfile,"%%%%EndComments\n");
  226.     fprintf(outfile,"/gnudict 40 dict def\ngnudict begin\n");
  227.     fprintf(outfile,"/Color %s def\n",ps_color ? "true" : "false");
  228.     fprintf(outfile,"/gnulinewidth %.3f def\n",PS_LW);
  229.     fprintf(outfile,"/vshift %d def\n", (int)(t->v_char)/(-3));
  230.     fprintf(outfile,"/dl {%d mul} def\n",PS_SC); /* dash length */
  231.     fprintf(outfile,"/hpt %.1f def\n",PS_HTIC/2.0);
  232.     fprintf(outfile,"/vpt %.1f def\n",PS_VTIC/2.0);
  233.     for ( i=0; PS_header[i] != NULL; i++)
  234.         fprintf(outfile,"%s",PS_header[i]);
  235.     fprintf(outfile,"end\n");
  236.     fprintf(outfile,"%%%%EndProlog\n");
  237. }
  238.  
  239.  
  240. PS_graphics()
  241. {
  242. struct termentry *t = &term_tbl[term];
  243.     ps_page++;
  244.     if (!ps_eps)
  245.         fprintf(outfile,"%%%%Page: %d %d\n",ps_page,ps_page);
  246.     fprintf(outfile,"gnudict begin\n");
  247.     fprintf(outfile,"gsave\n");
  248.     fprintf(outfile,"%d %d translate\n",PS_XOFF,PS_YOFF);
  249.     fprintf(outfile,"%.3f %.3f scale\n", (ps_eps ? 0.5 : 1.0)/PS_SC,
  250.                                          (ps_eps ? 0.5 : 1.0)/PS_SC);
  251.     if (!ps_portrait) {
  252.         fprintf(outfile,"90 rotate\n0 %d translate\n", (int)(-PS_YMAX*ysize));
  253.     }
  254.     fprintf(outfile,"0 setgray\n");
  255.     fprintf(outfile,"/%s findfont %d ",ps_font, (t->v_char) );
  256.     fprintf(outfile,"scalefont setfont\n");
  257.     fprintf(outfile,"newpath\n");
  258.     ps_path_count = 0;
  259.     PS_relative_ok = FALSE;
  260.     PS_pen_x = PS_pen_y = -4000;
  261.     PS_taken = 0;
  262.     PS_linetype_last = -1;
  263. }
  264.  
  265.  
  266. PS_text()
  267. {
  268.     ps_path_count = 0;
  269.     fprintf(outfile,"stroke\ngrestore\nend\nshowpage\n");
  270.     /* fprintf(stderr,"taken %d times\n",PS_taken); */
  271.     /* informational:  tells how many times it was "cheaper"
  272.        to do a relative moveto or lineto rather than an
  273.        absolute one */
  274. }
  275.  
  276.  
  277. PS_reset()
  278. {
  279.     fprintf(outfile,"%%%%Trailer\n");
  280.     if (!ps_eps)
  281.         fprintf(outfile,"%%%%Pages: %d\n",ps_page);
  282. }
  283.  
  284.  
  285. PS_linetype(linetype)
  286. int linetype;
  287. {
  288. char *line = "ba012345678"; 
  289.     linetype = (linetype % 9) + 2;
  290.     PS_relative_ok = FALSE;
  291.     if (PS_linetype_last == linetype) return(0);
  292.     PS_linetype_last = linetype;
  293.     fprintf(outfile,"LT%c\n", line[linetype]);
  294.     ps_path_count = 0;
  295. }
  296.  
  297.  
  298. PS_move(x,y)
  299. unsigned int x,y;
  300. {
  301.     int dx, dy;
  302.     char abso[20],rel[20];
  303.     dx = x - PS_pen_x;
  304.     dy = y - PS_pen_y;
  305.     /* can't cancel all null moves--need a move after stroke'ing */
  306.     if (dx==0 && dy==0 && PS_relative_ok)
  307.         return(0);
  308.     sprintf(abso, "%d %d M\n", x, y);
  309.     sprintf(rel, "%d %d R\n", dx, dy);
  310.     if (strlen(rel) < strlen(abso) && PS_relative_ok){
  311.         fputs(rel, outfile);
  312.         PS_taken++;
  313.     }else
  314.         fputs(abso, outfile);
  315.     PS_relative_ok = TRUE;
  316.     ps_path_count += 1;
  317.     PS_pen_x = x;
  318.     PS_pen_y = y;
  319. }
  320.  
  321. PS_vector(x,y)
  322. unsigned int x,y;
  323. {
  324.     int dx, dy;
  325.     char abso[20],rel[20];
  326.     dx = x - PS_pen_x;
  327.     dy = y - PS_pen_y;
  328.     if (dx==0 && dy==0) return(0);
  329.     sprintf(abso, "%d %d L\n", x, y);
  330.     sprintf(rel, "%d %d V\n", dx, dy);
  331.     if (strlen(rel) < strlen(abso) && PS_relative_ok){
  332.         fputs(rel, outfile);
  333.         PS_taken++;
  334.     }else
  335.         fputs(abso, outfile);
  336.     PS_relative_ok = TRUE;
  337.     ps_path_count += 1;
  338.     PS_pen_x = x;
  339.     PS_pen_y = y;
  340.     if (ps_path_count >= 400) {
  341.         fprintf(outfile,"currentpoint stroke M\n");
  342.         ps_path_count = 0;
  343.     }
  344. }
  345.  
  346.  
  347. PS_put_text(x,y,str)
  348. unsigned int x, y;
  349. char *str;
  350. {
  351. char ch;
  352.     if (!strlen(str)) return(0);
  353.     PS_move(x,y);
  354.     if (ps_ang != 0)
  355.         fprintf(outfile,"currentpoint gsave translate %d rotate 0 0 M\n"
  356.             ,ps_ang*90);
  357.     putc('(',outfile);
  358.     ch = *str++;
  359.     while(ch!='\0') {
  360.         if ( (ch=='(') || (ch==')') || (ch=='\\') )
  361.             putc('\\',outfile);
  362.         putc(ch,outfile);
  363.         ch = *str++;
  364.     }
  365.     switch(ps_justify) {
  366.         case LEFT : fprintf(outfile,") Lshow\n");
  367.             break;
  368.         case CENTRE : fprintf(outfile,") Cshow\n");
  369.             break;
  370.         case RIGHT : fprintf(outfile,") Rshow\n");
  371.             break;
  372.     }
  373.     if (ps_ang != 0)
  374.         fprintf(outfile,"grestore\n");
  375.     ps_path_count = 0;
  376.     PS_relative_ok = FALSE;
  377. }
  378.  
  379. int PS_text_angle(ang)
  380. int ang;
  381. {
  382.     ps_ang=ang;
  383.     return TRUE;
  384. }
  385.  
  386. int PS_justify_text(mode)
  387. enum JUSTIFY mode;
  388. {
  389.     ps_justify=mode;
  390.     return TRUE;
  391. }
  392.  
  393. /* postscript point routines */
  394. PS_point(x,y,number)
  395. int x,y;
  396. int number;
  397. {
  398. char *point = "PDABCTS";
  399.     number %= POINT_TYPES;
  400.      if (number < -1)
  401.         number = -1;        /* negative types are all 'dot' */
  402.     fprintf(outfile,"%d %d %c\n", x, y, point[number+1]);
  403.     PS_relative_ok = 0;
  404.     ps_path_count = 0;
  405. }
  406.  
  407.